import Vue from 'vue'
import VueRouter from 'vue-router'
import home from './home'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    meta: {
      requireAuth: true
    },
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      requireAuth: true
    },
    component: () => import('views/login/LoginMenu.vue')
  },
  home,
  {
    path: '/*',
    name: 'Error',
    component: () => import('views/Error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (!to.meta.requireAuth && !sessionStorage.loginAccount) {
    next('/login')
  }
  if (to.meta.requireAuth && sessionStorage.loginAccount) {
    next('/home/')
  }
  next()
})

export default router
